﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Text.RegularExpressions;

namespace GetWords
{
    public partial class Main : Form
    {
        private List<Word> words = new List<Word>();
        private string fileName = "单词列表.xls";
        //单词录音
        //http://baicizhan2.qiniudn.com/word_audios/frame.mp3
        public Main()
        {
            InitializeComponent();
            wb.ScriptErrorsSuppressed = true;
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser browser = (WebBrowser)sender;
            var body = browser.Document.Body;
            if (body.InnerHtml.Contains("开始背单词吧"))
            {
                string regStr = @"距离完成<span>(?<课程>\w+)";
                var reg = new Regex(regStr, RegexOptions.IgnoreCase);
                fileName = reg.Match(body.InnerHtml).Groups["课程"].Value + "_" + fileName;
                wb.Navigate("http://www.baicizhan.com/user/all_done_words_list?page=0");
            }

            if (body.InnerHtml.Contains("total_page"))
            {
                Result result = new JavaScriptSerializer().Deserialize<Result>(body.InnerHtml);
                words.AddRange(result.list);
                if (words.Count >= result.total_count)
                {
                    var outputResult = from word in words
                                       orderby word.wrong_times descending
                                       select word;

                    HSSFWorkbook hssfworkbook = new HSSFWorkbook();
                    ISheet sheet = hssfworkbook.CreateSheet();

                    IRow headerRow = sheet.CreateRow(0);
                    headerRow.CreateCell(0).SetCellValue("topic_id");
                    headerRow.CreateCell(1).SetCellValue("word");
                    headerRow.CreateCell(2).SetCellValue("word_meaning");
                    headerRow.CreateCell(3).SetCellValue("wrong_times");

                    int i = 0;
                    foreach (var output in outputResult)
                    {
                        IRow row = sheet.CreateRow(++i);
                        row.CreateCell(0).SetCellValue(output.topic_id);
                        row.CreateCell(1).SetCellValue(output.word);
                        row.CreateCell(2).SetCellValue(output.word_meaning);
                        row.CreateCell(3).SetCellValue(output.wrong_times);
                    }

                    FileStream file = new FileStream(fileName, FileMode.Create);

                    hssfworkbook.Write(file);

                    file.Close();
                    wb.Document.Write("已完成,结果存放在当前目录下 " + fileName);
                    return;
                }
                if (result.total_page > result.cur_page)
                    wb.Navigate("http://www.baicizhan.com/user/all_done_words_list?page=" + (result.cur_page + 1));
            }
        }
    }
}
